package com.soft.mainsite.module.data.web;

import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.domain.ErrorCode;
import com.soft.mainsite.common.exception.CmsException;
import com.soft.mainsite.common.utils.ExcelUtil;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.data.dto.AppointmentDto;
import com.soft.mainsite.module.data.dto.AppointmentQueryParams;
import com.soft.mainsite.module.data.entity.Appointment;
import com.soft.mainsite.module.data.service.AppointmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: hjm
 * @Date: 2020/11/28 23:19
 * Stay hungry,stay foolish!
 */
@Api(tags = "后台管理系统用户信息相关接口", value = "appointmentController")
@Controller
@Slf4j
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    @ApiOperation("分页条件查询预约列表")
    @RequestMapping("/list")
    public String index(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageNumber,
                        @RequestParam(value = "pageSize", defaultValue = "30") Integer pageSize,
                        AppointmentQueryParams queryParams, Model model) {
        model.addAttribute("appointmentList", appointmentService.pageAppointmentByConditions(queryParams, pageNumber, pageSize));
        model.addAttribute("pojo", queryParams);
        return "appointment/appointment_list";
    }

    @ApiOperation("删除预约")
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Long[] ids) {
        return appointmentService.delete(ids);
    }

    @ApiOperation("逻辑删除用户领取的资料")
    @RequestMapping("/update")
    @ResponseBody
    public String update(Long[] ids) {
        return appointmentService.update(ids);
    }

    @ApiOperation("办理预约")
    @RequestMapping("/do")
    @ResponseBody
    public String doAppointment(Long[] ids) {
        return appointmentService.doAppointment(ids);
    }

    @ApiOperation("导出预约Excel")
    @RequestMapping("/excel")
    @ResponseBody
    public String excel(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageNumber,
                        @RequestParam(value = "pageSize", defaultValue = "30") Integer pageSize,
                        AppointmentQueryParams queryParams) {

        PageInfo<AppointmentDto> appointmentDtoPageInfo = appointmentService.pageAppointmentByConditions(queryParams, pageNumber, pageSize);
        List<AppointmentDto> list = appointmentDtoPageInfo.getList();
        if (list.isEmpty()) {
            log.error(ErrorCode.E_100301.getDesc(), list, list.size());
            return JsonUtil.toError(ErrorCode.E_100301.getDesc());
        }

        List<Appointment> appointments = AppointmentDto.convertDtoToEntitys(list);
        try {
            ExcelUtil.exports2007("预约信息", appointments);
        } catch (CmsException e) {
            log.error(ErrorCode.E_100302.getDesc(), e);
            return JsonUtil.toError(ErrorCode.E_100302.getDesc());
        }

        return JsonUtil.toSuccess("操作成功");
    }
}
